package com.singleton;

/**
 * 懒汉式
 */
public class Singleton1 {

    // 1. 构造方法私有化
    private Singleton1() {
    }
//2、然后声明一个静态变量保存单例的引用
    public static Singleton1 singleton1 = null;

    //3.静态方法获取实例
    ////不安全的
    public static Singleton1 getInstance(){
        if (singleton1==null){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            singleton1=new Singleton1();
        }
        return singleton1;
    }
}

class testSingleton1{
    public static void main(String[] args) {
//        System.out.println(Singleton1.getInstance().hashCode());
//        System.out.println(Singleton1.getInstance().hashCode());
//        System.out.println(Singleton1.getInstance().hashCode());
        for (int i = 0; i < 3; i++) {
             new Thread(new Runnable() {
                 @Override
                 public void run() {
                     System.out.println(Singleton1.getInstance().hashCode());
                 }
             }).start();;
        }
    }
}
